<?php
/**
 * 食材服务模块
 * 
 * @author river_he@eueang.com
 */
include_once(AMFPHP_BASE . "shared/util/MethodTable.php");
final class MaterialService extends Kit_Service{
	/**
	 * 获取食材列表
	 * 
	 * @param $page
	 * @param $psize
	 * @return array
	 */
	public function getMaters($page, $psize){
		$page = $page>0?$page:1;
		$psize = $psize>0?$psize:10;
		$key = __FUNCTION__.'_'.$page.'_'.$psize;
		$key = $this->getCacheKey($key);
		
		$data = $this->getMaterMode()->getItems('1=1', 'nHeat ASC', $page, $psize);
		if($data['data']){
			return $this->pack(200, 'OK', $data);
		}
		return $this->pack(201, 'not found data');
	}
	
	/**
	 * 购买装修家居
	 * 
	 * @param int $shoperId 店主id
	 * @param int $shopId 店铺id
	 * @param int $decoId 家居id
	 * @return array
	 */
	public function buy($shoperId, $mid, $total){
		if(1 > $shoperId){
			return $this->pack(101, 'argument shoperId error');
		}elseif(1 > $total){
			return $this->pack(102, 'argument total error');
		}elseif(1 > $mid){
			return $this->pack(103, 'argument mid error');
		}
		$buy = $this->getMaterMode()->buy($shoperId, $mid, $total);
		if(is_array($buy)){
			return $this->pack(200, 'OK', $buy);
		}else{
			$message = '';
			$code = 0;
			switch($buy){
				case -1:
					$message .= 'mater not found';
					$code = 201;
					break;
				case -2:
					$message .= 'shoper not found';
					$code = 202;
					break;
				case -3:
					$message .= '食材定价错误';
					$code = 205;
					break;
				case -4:
					$message .= '金币不足';
					$code = 206;
					break;
				case -5:
					$message .= '美食币不足';
					$code = 207;
					break;
				case -6:
					$message .= '数据处理错误';
					$code = 208;
					break;
				case -7:
					$message .= '数据处理错误';
					$code = 209;
					break;
			}
			return $this->pack($code, $message);
		}
	}
	
	/**
	 * 卖出道具
	 * 
	 * @param int $shoperId 店主id
	 * @param int $shoperMaterId 店主食材id
	 * @param int $total 销售数量
	 * @return array
	 */
	public function sell($shoperId, $shoperMaterId, $total){
		if(1 > $shoperId){
			return $this->pack(101, 'argument shoperId error');
		}elseif(1 > $shoperMaterId){
			return $this->pack(102, 'argument shoperMaterId error');
		}elseif(1 > $total){
			return $this->pack(103, 'argument total error');
		}
		$sell = $this->getMaterMode()->sell($shoperId, $shoperMaterId, $total);
		if(0 < $sell){
			return $this->pack(200, 'OK');
		}else{
			$code = 0;
			$message = '';
			switch($sell){
				case -1:
					$code += 201;
					$message .= 'no property'; 
					break;
				case -2:
					$code += 202;
					$message .= 'shoper id wrong';
					break;
				case -3:
					$code += 203;
					$message .= '该道具已经使用或已经卖出';
					break;
				case -4:
					$code += 204;
					$message .= 'shoper id wrong';
					break;
				case -5:
					$code += 205;
					$message .= 'operation error';
					break;
				case -6:
					$code += 206;
					$message .= 'server error';
					break;
			}
			return $this->pack($code, $message);
		}
		return $this->pack(501, 'server error');
	}
}
//end